package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.common.utils.RDateUtils;
import com.rockcent.mall.lib.domain.enumclass.MerchantStatus;
import com.rockcent.mall.lib.dto.MerchantCertifyDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * Created by zhangjh on 2016/10/9.
 */

@Repository
public class MallMerchantCertifyRepositoryImpl implements MallMerchantCertifyRepositoryCustom {

    private static final Logger log = LoggerFactory.getLogger(MallMerchantCertifyRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    private ObjectMapper objectMapper;


    @Override
    public PageResult<MerchantCertifyDto> findVerifiedMerchant(String name, String commitStartDate, String commitEndDate, String type, Boolean isVerify, String status, Long mallId, PageResult pageResult, String realName, String contactPhone) {
        SQLPageDoing doing = (sql, fields, params, typeReferenceMap) -> {
            // a.status,a.name,a.mobile ,b.last_updated ,b.type
            sql.append("SELECT %s FROM  merchant_certify b " +
                    " LEFT JOIN merchant a ON b.merchant_id=a.id  where a.mall_id=:mallId");
            params.put("mallId", mallId);
            //是否已经审核
            if (isVerify != null) {
                if (isVerify == Boolean.TRUE) {
                    if (status == null) {
                        sql.append(" AND a.status in('VERIFIED','DEVERIFY','PROBATION','DISABLED','NORMAL')");
                    }else{
                        if(MerchantStatus.VERIFIED.toString().equals(status)){
                            sql.append(" AND a.status in('VERIFIED','PROBATION','DISABLED','NORMAL')");

                        }else  if(MerchantStatus.DEVERIFY.toString().equals(status)){
                            sql.append(" AND a.status ='DEVERIFY' ");

                        }
                    }
                } else {
                    sql.append(" AND a.status in('VERIFING','UNVERIFY')");

                }
            }

            sql.append(" AND (a.`is_delete`='NO')");


            if (StringUtils.isNotBlank(name)) {
                sql.append(" AND a.name like:name");
                params.put("name", "%" + name + "%");
            }
            if (StringUtils.isNotBlank(type)) {
                sql.append(" AND b.type =:type");
                params.put("type", type);
            }

            if(StringUtils.isNotEmpty(realName)) {
                sql.append(" AND b.real_name =:realName");
                params.put("realName", realName);
            }

            if(StringUtils.isNotEmpty(contactPhone)) {
                sql.append(" AND b.contact_phone =:contactPhone");
                params.put("contactPhone", contactPhone);
            }

            if (isVerify != null && isVerify == Boolean.FALSE) {
                if (StringUtils.isNotBlank(commitStartDate) && StringUtils.isNotBlank(commitEndDate)) {
                    sql.append(" AND b.last_updated >=:commitStartDate AND b.last_updated <:commitEndDate");
                    params.put("commitStartDate", RDateUtils.beforeDateStr(commitStartDate, 0));
                    params.put("commitEndDate", RDateUtils.afterDateStr(commitEndDate, 1));

                } else if (StringUtils.isNotBlank(commitStartDate)) {
                    sql.append(" AND b.last_updated >=:commitStartDate");
                    params.put("commitStartDate", RDateUtils.beforeDateStr(commitStartDate, 0));
                } else if (StringUtils.isNotBlank(commitEndDate)) {
                    sql.append(" AND b.last_updated <:commitEndDate");
                    params.put("commitEndDate", RDateUtils.afterDateStr(commitEndDate, 1));
                }
            }

            if (isVerify != null && isVerify == Boolean.TRUE) {
                if (StringUtils.isNotBlank(commitStartDate) && StringUtils.isNotBlank(commitEndDate)) {
                    sql.append(" AND b.date_audited >= :commitStartDate AND b.date_audited < :commitEndDate");
                    params.put("commitStartDate", RDateUtils.beforeDateStr(commitStartDate, 0));
                    params.put("commitEndDate", RDateUtils.afterDateStr(commitEndDate, 1));

                } else if (StringUtils.isNotBlank(commitStartDate)) {
                    sql.append(" AND b.date_audited >= :commitStartDate");
                    params.put("commitStartDate", RDateUtils.beforeDateStr(commitStartDate, 0));
                } else if (StringUtils.isNotBlank(commitEndDate)) {
                    sql.append(" AND b.date_audited < :commitEndDate");
                    params.put("commitEndDate", RDateUtils.afterDateStr(commitEndDate, 1));
                }
            }

            if (isVerify == Boolean.TRUE) {
                sql.append(" ORDER BY b.date_audited desc");
            } else {
                sql.append("  ORDER BY b.last_updated desc");

            }
            if (isVerify != null) {
                if (isVerify == Boolean.TRUE) {
                    fields.append("b.id,b.real_name,a.status,a.name,a.mobile ,b.date_audited ,b.type,b.contact_phone,(select count(1) from favorite f where b.merchant_id=f.merchant_id and f.type='MERCHANT') focus_num");
                }else {
                    fields.append("b.id,b.real_name,a.name,a.mobile ,b.date_created ,b.type,b.contact_phone");

                }
            }
            log.debug(sql.toString());

        };
        return doing.go(pageResult, MerchantCertifyDto.class, objectMapper, jdbcTemplate, log);
    }


}

